package com.elinkway.infinitemovies.view;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.elinkway.infinitemovies.adaction.CommonAdActionHandle;
import com.elinkway.infinitemovies.async.ThirdAdReportTask;
import com.elinkway.infinitemovies.bean.XfAdBean;
import com.elinkway.infinitemovies.bean.YzAdBean;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.SelfDataConstant;
import com.elinkway.infinitemovies.selfdata.bean.MyDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Author: liuxiaojiang(xiao)
 * Date: 2017-06-05
 * Time: 14:57
 * Description: 讯飞广告展示的view
 */
public class XfPayAdView {
    private Activity activity;
    private View mView;
    private long startRequestTime = 0;
    private String mAdposition;
    ArrayList<String> adShowReportUrls = null;
    ArrayList<String> adClickReportUrls = null;
    ArrayList<String> downloadReportUrls = null;
    ArrayList<String> downloadedReportUrls = null;
    ArrayList<String> installReportUrls = null;
    ArrayList<String> installedReportUrls = null;
    private boolean mIsClickReported = false;

    public XfPayAdView(Activity activity, View view, String mAdposition) {
        this.activity = activity;
        this.mView = view;
        this.mAdposition = mAdposition;
    }

    public XfPayAdView(Activity activity, String mAdposition) {
        this.activity = activity;
        this.mAdposition = mAdposition;
    }

    public void setXfAd(final XfAdBean xfAdBean) {
        mIsClickReported = false;
        if (mView == null) {
            mView = activity.findViewById(R.id.nativeADContainerone);
        }
        if (xfAdBean.batch_ma == null) {
            return;
        }
        adShowReportUrls = xfAdBean.batch_ma.impr_url;
        adClickReportUrls = xfAdBean.batch_ma.click_url;
        downloadReportUrls = xfAdBean.batch_ma.inst_downstart_url;
        downloadedReportUrls = xfAdBean.batch_ma.inst_downsucc_url;
        installReportUrls = xfAdBean.batch_ma.inst_installstart_url;
        installedReportUrls = xfAdBean.batch_ma.inst_installsucc_url;

        String imageUrl = xfAdBean.batch_ma.image;
        final String adTitle = xfAdBean.batch_ma.title;
        String adDesc = xfAdBean.batch_ma.sub_title;
        final String iconUrl = xfAdBean.batch_ma.icon;
        final String deeplink = xfAdBean.batch_ma.deep_link;
        final String appname = xfAdBean.batch_ma.package_name;
        final String adType = xfAdBean.batch_ma.adtype;
        final String adUrl = xfAdBean.batch_ma.landing_url;

        AQuery aq = new AQuery(activity, mView);
        mView.setVisibility(View.VISIBLE);
        if ("pause".equals(mAdposition)) {
            mView.findViewById(R.id.pause_ad_image).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.ad_close_iv).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.ad_logo_iv).setVisibility(View.GONE);
            TextView tipsTv = (TextView) mView.findViewById(R.id.ad_tips_tv);
            RelativeLayout.LayoutParams tipsParams = (RelativeLayout.LayoutParams) tipsTv.getLayoutParams();
            tipsParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.pause_ad_image);
            tipsTv.setLayoutParams(tipsParams);
            tipsTv.setVisibility(View.VISIBLE);
            mView.findViewById(R.id.baidu_inter_ad_rl).setVisibility(View.GONE);
            if (!TextUtils.isEmpty(imageUrl)) {
                aq.id(R.id.pause_ad_image).image(imageUrl);
            }
            mView.findViewById(R.id.ad_close_iv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mView.setVisibility(View.GONE);
                    reportAdClose();
                }
            });
        } else if ("splash".equals(mAdposition)) {//启动图
            aq.id(R.id.splash_ad_iv).visible();
            aq.id(R.id.splash_ad_iv).image(imageUrl, false, true);
            activity.findViewById(R.id.nativeADContainer).setVisibility(View.VISIBLE);
        } else if ("focus".equals(mAdposition)) {
            aq.id(R.id.ad_text_mark_tv_one).visible();
            if (!TextUtils.isEmpty(imageUrl)) {
                aq.id(R.id.focus_item_image).image(imageUrl);
            }
            aq.id(R.id.focus_item_tv).text(adTitle);
        } else if("homebanner".equals(mAdposition)){
            mView.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIs.getScreenWidth()/2);
            mView.findViewById(R.id.home_banner_ad).setLayoutParams(params);
            aq.id(R.id.text_banner_desc).text(adDesc);
            aq.id(R.id.home_banner_ad).image(imageUrl);
            aq.id(R.id.ad_text_mark_tv_banner).visibility(View.GONE);
            aq.id(R.id.server_ad_tips).visibility(View.VISIBLE);
        } else if ("exit".equals(mAdposition)) {
            aq.id(R.id.ad_tips_tv).visible();
            aq.id(R.id.iv_main).image(imageUrl, false, true);
        } else if ("pre-roll".equals(mAdposition)) {
            mView.setVisibility(View.VISIBLE);
            mView.findViewById(R.id.iv_attach_photo).setVisibility(View.VISIBLE);
            aq.id(R.id.iv_attach_photo).image(imageUrl);
        }else {
            if (!TextUtils.isEmpty(imageUrl)) {
                aq.id(R.id.ad_left_image_iv_one).image(imageUrl);
            }
            aq.id(R.id.text_desc_one).text(adDesc);
            aq.id(R.id.text_title_one).text(adTitle);
            aq.id(R.id.ad_text_mark_tv_one).visibility(View.GONE);
            aq.id(R.id.server_ad_tips).visibility(View.VISIBLE);
        }
        if(!"focus".equals(mAdposition)){
            reportAdShow();
        }
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击响应
                if (!TextUtils.isEmpty(adUrl)) {
                    if("download".equals(adType)){
                        CommonAdActionHandle.getInstance().downloadApp(activity, appname, adTitle, iconUrl,
                                adUrl, deeplink, downloadReportUrls, downloadedReportUrls,
                                installReportUrls, installedReportUrls);
                    } else {
                        if(!TextUtils.isEmpty(deeplink)){
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(deeplink));
                            if (Utils.deviceCanHandleIntent(activity, intent)) {
                                activity.startActivity(intent);
                            } else {
                                CommonAdActionHandle.getInstance().go2WebView(activity, adUrl);
                            }
                        }else{
                            CommonAdActionHandle.getInstance().go2WebView(activity, adUrl);
                        }
                    }
                    if(!mIsClickReported){
                        reportAdClick();
                        mIsClickReported = true;
                    }
                }
            }
        });
    }

    private void reportAdShow() {
        if(adShowReportUrls != null && adShowReportUrls.size() > 0){
            for (int i=0;i<adShowReportUrls.size();i++){
                if(!TextUtils.isEmpty(adShowReportUrls.get(i))){
                    new ThirdAdReportTask(activity, adShowReportUrls.get(i), Utils.getBrowserUa(activity)).start();
                }
            }
        }
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.XFSERVERAD_SHOW, gdtPropertyMap);

        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(SelfDataConstant.AC_ADSHOW);
        record.setAd_pro(SelfDataConstant.AD_XF);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }

    private void reportAdClick() {
        if(adClickReportUrls != null && adClickReportUrls.size() > 0){
            for (int i=0;i<adClickReportUrls.size();i++){
                String oneReportUrl = adClickReportUrls.get(i);
                if(!TextUtils.isEmpty(oneReportUrl)){
                    if(mView instanceof ClickRelativeLayout){
                        oneReportUrl = oneReportUrl.replace("IT_CLK_PNT_DOWN_X", ((ClickRelativeLayout) mView).mDownX+"").replace("IT_CLK_PNT_DOWN_Y", ((ClickRelativeLayout) mView).mDownY+"")
                                .replace("IT_CLK_PNT_UP_X", ((ClickRelativeLayout) mView).mUpX+"").replace("IT_CLK_PNT_UP_Y", ((ClickRelativeLayout) mView).mUpY+"");
                    }
                    new ThirdAdReportTask(activity, oneReportUrl, Utils.getBrowserUa(activity)).start();
                }
            }
        }
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.XFSERVERAD_CLICK, gdtPropertyMap);

        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(SelfDataConstant.AC_ADCLICK);
        record.setAd_pro(SelfDataConstant.AD_XF);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }

    private void reportAdClose() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.XFSERVERAD_CLOSE, gdtPropertyMap);
    }
}
